package org.eduapp.trains.service.order.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.eduapp.trains.DAOException;
import org.eduapp.trains.ServiceException;
import org.eduapp.trains.dao.order.OrderDAO;
import org.eduapp.trains.model.account.ModelAccount;
import org.eduapp.trains.model.order.ModelOrder;
import org.eduapp.trains.service.order.OrderService;
import org.eduapp.trains.utils.UtilsString;
import org.eduapp.trains.web.support.PaginationSupport;

/**
 * 
 * @author jeccy
 *
 */
public class OrderServiceImpl implements OrderService {
	
	private final static Logger logger = Logger.getLogger(OrderServiceImpl.class);
			
	private OrderDAO orderDAO;

	@Override
	public ModelOrder get(String id) throws ServiceException {
		if (UtilsString.isNotEmpty(id)) {
			try {
				return orderDAO.get(id);
			} catch (DAOException e) {
				logger.error("get order by id" + id + ",caused by:" + e.getMessage());
				e.printStackTrace();
			} catch (Exception e) {
				logger.error("get order by id" + id + ",caused by:" + e.getMessage());
				throw (new ServiceException(e));
			}
		} 
		return null;
	}

	@Override
	public PaginationSupport<ModelOrder> get(ModelOrder model, int pageSize,
			int offset, String order, Boolean isDesc) throws ServiceException {
		try {
			return orderDAO.get(model, pageSize, offset, order, isDesc);
		} catch (DAOException e) {
			logger.error("get orders by pager failed ,caused by:" + e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			logger.error("get orders by pager failed ,caused by:" + e.getMessage());
			throw (new ServiceException(e));
		}
		return null;
	}
	
	@Override
	public PaginationSupport<ModelOrder> get(ModelOrder model,String ids, int pageSize,
			int offset, String order, Boolean isDesc) throws ServiceException {
		try {
			return orderDAO.get(model, ids, pageSize, offset, order, isDesc);
		} catch (DAOException e) {
			logger.error("get orders by pager failed ,caused by:" + e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			logger.error("get orders by pager failed ,caused by:" + e.getMessage());
			throw (new ServiceException(e));
		}
		return null;
	}

	@Override
	public boolean remove(String id) throws ServiceException {
		if (UtilsString.isNotEmpty(id)) {
			ModelOrder model = get(id);
			if (model != null) {
				try {
					orderDAO.remove(model);
					return true;
				} catch (DAOException e) {
					logger.error("Remove order failed with id" + id + ",caused by:" + e.getMessage());
					e.printStackTrace();
				} catch (Exception e) {
					logger.error("Remove order failed with id" + id + ",caused by:" + e.getMessage());
					throw (new ServiceException(e));
				}
			} else {
				logger.info("Remove order failed, id with " + id + "not found");
			}
		}
		logger.info("Remove order failed, empty id found");
		return false;
	}

	@Override
	public boolean saveOrUpdate(ModelOrder model) throws ServiceException {
		if (model != null) {
			try {
				orderDAO.saveOrUpdate(model);
				return true;
			} catch (DAOException e) {
				logger.error("SaveOrUpdate order failed with model" + model.toString() + ",caused by:" + e.getMessage());
				e.printStackTrace();
			} catch (Exception e) {
				logger.error("SaveOrUpdate order failed with model" + model.toString() + ",caused by:" + e.getMessage());
				throw (new ServiceException(e));
			}
		}
		logger.info("SaveOrUpdate order failed, empty model found");
		return false;
	}

	public void setOrderDAO(OrderDAO orderDAO) {
		this.orderDAO = orderDAO;
	}

	public OrderDAO getOrderDAO() {
		return orderDAO;
	}

	public static Logger getLogger() {
		return logger;
	}

}
